/*
 *  $Id: BackToBack.cs 1004 2010-05-03 11:50:53Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Earth.Abilities {
    public class BackToBack: AbilityCard<Creature> {
        public BackToBack(): base("001_e_a_back_to_back") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Earth;
            Target = CreatureTarget.OwnExceptSelf;
            LogMessage = GetLogPhrase();
            AllowInOffence = false;
            AllowInDefence = true;
        }

        protected override void LogAction(Log log, TargetArgs<Creature> args) {
            log.Add(LogMessage, args.Caster, this, args.Target);
        }

        protected override void DoAction(TargetArgs<Creature> args) {
            args.Target.AddAffect(new BackToBackAffect(args.Caster));
        }

        private class BackToBackAffect: SpellAffect<BackToBack> {
            private AbstractObject _supporter;
            private int _addHealth, _prevHealth;

            public BackToBackAffect(AbstractObject supporter) {
                _supporter = supporter;
                _addHealth = supporter.Health;
                Add(new TimeAffect(1, new Affects.Guardian()));
            }

            public override Affect Clone() {
                return new BackToBackAffect(_supporter);
            }

            public override void SetAffect() {
                base.SetAffect();
                if (_addHealth > 0) {
                    _prevHealth = Owner.Health;
                    LocalSummary.ChangeMaxHealth(_addHealth);
                }
            }
            public override void ResetAffect() {
                base.ResetAffect();
                if (_addHealth > 0) {
                    int dmg = _prevHealth - Owner.Health;
                    if (dmg > 0) _supporter.DamageDirectBy(NewDamage(DamageType.Health, dmg), false);
                    LocalSummary.ChangeMaxHealth(-_addHealth);
                }
            }
        }
    }
}
